package com.dhl.springbootrabbitmq.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author danghailiang
 */
@Configuration
public class TtlQueueConfig {
    /**
     * X交换机
     */
    public static final String X_CHANGE = "X";
    /**
     * Y交换机
     */
    public static final String Y_CHANGE = "Y";
    /**
     * QA队列
     */
    public static final String QA_QUEUE = "QA";
    /**
     * QB队列
     */
    public static final String QB_QUEUE = "QB";
    /**
     * QD 死信队列
     */
    public static final String QD_DEAD_QUEUE = "QD";
    /**
     * 路由keyXA
     */
    public static final String ROUTING_XA = "XA";
    /**
     * 路由keyXB
     */
    public static final String ROUTING_XB = "XB";
    /**
     * 路由keyYD
     */
    public static final String ROUTING_YD = "YD";

    /**
     * QC队列
     */
    public static final String QC_QUEUE = "QC";
    /**
     * 路由keyXC
     */
    public static final String ROUTING_XC = "XC";

    /**
     * 声明 X 直接 交换
     */
    @Bean
    public DirectExchange xExchange() {
        return ExchangeBuilder.directExchange(X_CHANGE).build();
    }

    /**
     * 声明 Y 直接 交换
     */
    @Bean
    public DirectExchange yExchange() {
        return ExchangeBuilder.directExchange(Y_CHANGE).build();
    }

    /**
     * 声明 QA 队列
     */
    @Bean
    public Queue queueA() {
        return QueueBuilder.durable(QA_QUEUE)
                .ttl(20000)
                .deadLetterExchange(Y_CHANGE)
                .deadLetterRoutingKey(ROUTING_YD)
                .build();
    }
    /**
     * 声明 QB 队列
     */
    @Bean
    public Queue queueB() {
        return QueueBuilder.durable(QB_QUEUE)
                .ttl(50000)
                .deadLetterExchange(Y_CHANGE)
                .deadLetterRoutingKey(ROUTING_YD)
                .build();
    }
    /**
     * 声明 QD 死信队列
     */
    @Bean
    public Queue queueD() {
        return QueueBuilder.durable(QD_DEAD_QUEUE).build();
    }

    /**
     * 队列绑定交换
     * @param queueA 队列A
     * @param xExchange 交换X
     * @return
     */
    @Bean
    public Binding queueABindX(Queue queueA,DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with(ROUTING_XA);
    }
    /**
     * 队列绑定交换
     * @param queueB 队列B
     * @param xExchange 交换X
     * @return
     */
    @Bean
    public Binding queueBBindX(Queue queueB,DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with(ROUTING_XB);
    }
    /**
     * 队列绑定交换
     * @param queueD 队列D
     * @param yExchange 交换Y
     * @return
     */
    @Bean
    public Binding queueDBindY(Queue queueD,DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with(ROUTING_YD);
    }

    /**
     * 声明QC队列
     * @return
     */
    @Bean
    public Queue queueC(){
        return QueueBuilder.durable(QC_QUEUE)
                .deadLetterRoutingKey(ROUTING_YD)
                .deadLetterExchange(Y_CHANGE)
                .build();
    }
    /**
     * 队列绑定交换
     * @param queueC 队列D
     * @param xExchange 交换Y
     * @return
     */
    @Bean
    public Binding queueCBindX(Queue queueC,DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with(ROUTING_XC);
    }
}

